"use strict";
require(["../../js/require.config.js"], function () {
    require(["jquery", "template","global"], function ($, template,global) {
        if(!global.existLoginUser()){
            global.goToLoginHtml();
        }
        var loginUser = global.getLoginUser();
        var app = {
            init: function () {
                var html=`
                <ul class="side-list open">
                    {{each data as val i}}
                        <li class="menu-wrap {{if id==i}}active fold{{/if}}" menuId="{{i}}">
                            <a class="menu-title" {{if val.url!=""}}href="{{val.url}}"{{/if}}>
                                <i class="menu-icon {{val.icon}}"></i>{{val.name}}
                            </a>
                            {{if val.children.length!=0}}
                                {{if val.children}}
                                    <div class="menu-child">
                                        <ul class="menu-child-list">
                                        {{each val.children as item j}}
                                            <li class="child-name {{if item.type=="项目" && pid==j}}active{{else if item.type!="项目" && cid==j}}active{{/if}}"><a class="sub-menu text-overflow {{if item.type=="项目"}}project-item{{/if}}" menuId="{{j}}" {{if item.type=="项目"}}projectId="{{item.id}}"{{/if}} {{if item.url!=""}}href="{{item.url}}"{{else}}href="javascript:void(0)"{{/if}} title={{item.name}}>{{item.name}}</a></li>
                                        {{/each}}
                                        </ul>
                                    </div>
                                {{/if}}
                            {{/if}}
                        </li>
                    {{/each}}
                </ul>
                <div class="side-arrow"><i class="aidicon aidicon-chevron-double-left"></i></div>
                `;
                global.get("/web/menu/left/"+loginUser.id,function(response) {
                    if(response.success){
                        var menus = response.data.menus;
                        var tplHtml = template.compile(html);
                        $(".side-menu").html(tplHtml({
                            "data": menus,
                            "id": localStorage.getItem("id"),
                            "cid": localStorage.getItem("cid"),
                            "pid":localStorage.getItem("pid")
                        }));
                        if(localStorage.getItem("id")==null || localStorage.getItem("cid")==null ||localStorage.getItem("pid")==null){
                            localStorage.setItem("id",0);
                            localStorage.setItem("cid",0);
                            localStorage.setItem("pid",0);
                            $(".project-item:first").click()
                        }
                    }else{
                        alert(response.message);
                    }
                    
                },null,null,false)

                var html2 = `
                    {{each data as val i}}
                        <li {{if class="{{if pageId==i}}active{{/if}}"><a menuId="{{i}}" href="{{val.url}}">{{val.name}}</a></li>
                    {{/each}}
                `;

                global.get("/web/menu/top/"+loginUser.id,function(response) {
                    if(response.success){
                        var menus = response.data.menus;
                        var tplHtml = template.compile(html2);
                        $(".top-menu").html(tplHtml({
                            "data": menus,
                            "pageId": $("#pageId").attr("pageId"),
                        }));
                    }else{
                        alert(response.message);
                    }
                })
                    
                // })
                // 退出
                $(document).on("click", ".logout", function () {
                    Dialog.open({
                        dragable: false,
                        closable: false,
                        theme: "logOutDia",
                        id: "logOutDia",
                        title: "<span class='aidicon aidicon-alert-circle-outline aidicon-warning'></span><span class='lineClamp2'>退出系统提醒</span>",
                        width: 320,
                        height: 148,
                        modal: true,
                        content: "<div class=\"padding-left-large-5\"><div class=\"font-color-weak padding-horizontal-large-2 padding-bottom-large-2\">确定退出系统吗？</div></div>",
                        button: [
                            {
                                id: "mybtn1", label: "确认", intent: "primary", focus: true, click: function () {
                                    localStorage.clear();
                                    window.history.replaceState(null, "", '/login.html');
                                    location.reload()
                                }
                            },
                            {
                                id: "mybtn2", label: "取消", click: function () {
                                    this.close();
                                }
                            }
                        ]
                    });
                });
                // 菜单展开收起
                $(document).on("click", ".side-menu .menu-wrap", function () {
                    var _this = $(this);
                    _this.addClass("active").siblings(".menu-wrap").removeClass("active");
                    if(_this.hasClass("fold")) {
                        _this.find(".menu-child").slideUp();
                        _this.removeClass("fold");
                    } else {
                        _this.find(".menu-child").slideDown();
                        _this.addClass("fold");
                    }
                    localStorage.setItem("id",_this.attr("menuId"));
                });
                // 点击子菜单
                $(document).on("click", ".side-menu .sub-menu", function () {
                    var _this = $(this);
                    //点击项目子菜单
                    if(_this.hasClass("project-item")){
                        var projectId = _this.attr("projectId");
                        global.get("/web/project/"+projectId,function (response) {
                            var project = response.data.project;
                            project.index = _this.attr("menuId")
                            localStorage.setItem("project",JSON.stringify(project));
                            location.href = "/system/main.html";
                        })
                        localStorage.setItem("pid",_this.attr("menuId"))
                        localStorage.setItem("cid",-1)
                    }else{
                        localStorage.setItem("cid",_this.attr("menuId"))
                    }
                });
                $(document).on("click", ".side-menu .side-arrow", function() {
                    $(".side-menu .side-list").toggle("fast");
                    var $aidicon = $(this).find(".aidicon")
                    if($aidicon.hasClass("aidicon-chevron-double-left")) {
                        $aidicon.removeClass("aidicon-chevron-double-left").addClass("aidicon-chevron-double-right");
                    } else {
                        $aidicon.removeClass("aidicon-chevron-double-right").addClass("aidicon-chevron-double-left");
                    }
                })
                //点击顶部菜单
                $(document).on("click", ".top-menu li a", function () {
                    localStorage.setItem("id",0);
                    localStorage.setItem("cid",localStorage.getCurrProject().index)
                    localStorage.setItem("pid",localStorage.getCurrProject().index)
                });

                $(document).on("click", ".logo-img", function () {
                    window.location.href = "/bigScreen.html";
                });
            }
        };
        app.init();
    
    })
})